/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.controller.util;

import com.googlecode.bambole.domain.Configuration;
import com.googlecode.bambole.facade.ConfigurationFacade;
import com.googlecode.bambole.util.SupportedLocalesUtil;
import com.googlecode.bambole.util.SupportedThemesUtil;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
public class ConfigurationUtil {

    private static ConfigurationFacade lookupConfigurationFacadeBean() {
        try {
            Context c = new InitialContext();
            return (ConfigurationFacade) c.lookup("java:global/Bambole/ConfigurationFacade!com.googlecode.bambole.facade.ConfigurationFacade");
        } catch (NamingException ne) {
            throw new RuntimeException(ne);
        }
    }

    private static ConfigurationFacade getConfigurationFacade() {
        return lookupConfigurationFacadeBean();
    }

    public static boolean isCheckCnpjCpf() {
        Configuration configuration = getConfigurationFacade().findByName("CheckCnpjCpf");
        return configuration == null ? false : configuration.isEnabled();
    }

    public static Locale getDefaultLocale() {
        Configuration configuration = getConfigurationFacade().findByName("DefaultLocale");
        if (configuration != null && configuration.isEnabled() && configuration.getDescription() != null && !configuration.getDescription().isEmpty()) {
            return SupportedLocalesUtil.getItems().containsValue(Locale.forLanguageTag(configuration.getDescription())) ? Locale.forLanguageTag(configuration.getDescription()) : Locale.forLanguageTag("pt-BR");
        }
        return Locale.forLanguageTag("pt-BR");
    }

    public static String getDefaultTheme() {
        Configuration configuration = getConfigurationFacade().findByName("DefaultTheme");
        if (configuration != null && configuration.isEnabled() && configuration.getDescription() != null && !configuration.getDescription().isEmpty()) {
            return SupportedThemesUtil.getItems().contains(configuration.getDescription()) ? configuration.getDescription() : "aristo";
        }
        return "aristo";
    }

    public static int getOrderDocumentNumber() {
        Configuration configuration = getConfigurationFacade().findByName("OrderDocumentNumber");
        if (configuration != null && configuration.isEnabled() && configuration.getDescription() != null && !configuration.getDescription().isEmpty()) {
            try {
                int documentNumber = Integer.parseInt(configuration.getDescription()) + 1;
                configuration.setDescription(String.valueOf(documentNumber));
                getConfigurationFacade().edit(configuration);
                return documentNumber <= 0 ? 1 : documentNumber;
            } catch (NumberFormatException e) {
                Logger.getAnonymousLogger().log(Level.INFO, "OrderDocumentNumber not set: {0}", e.getMessage());
                return -1;
            }
        }
        return -1;
    }

    public static String[] getSmtpSettings() {
        Configuration configuration = getConfigurationFacade().findByName("SmtpSettings");
        if (configuration != null && configuration.isEnabled() && configuration.getDescription() != null && !configuration.getDescription().isEmpty()) {

            String[] smtpSettings = configuration.getDescription().split("\\|");

            if (smtpSettings.length != 5) {
                Logger.getAnonymousLogger().log(Level.SEVERE, "Invalid SMTP settings");
                return null;
            } else {
                return smtpSettings;
            }
        }
        Logger.getAnonymousLogger().log(Level.SEVERE, "SMTP settings not set");
        return null;
    }

    public static int getSessionTimeout() {
        Configuration configuration = getConfigurationFacade().findByName("SessionTimeout");
        if (configuration != null && configuration.isEnabled() && configuration.getDescription() != null && !configuration.getDescription().isEmpty()) {
            try {
                int sessionTimeout = Integer.parseInt(configuration.getDescription());
                return sessionTimeout <= 0 ? 60000 : sessionTimeout * 60000;
            } catch (NumberFormatException e) {
                Logger.getAnonymousLogger().log(Level.INFO, "SessionTimeout not set: {0}", e.getMessage());
                return 60000;
            }
        }
        return 60000;
    }
}
